<?php
/**
 * 考勤签到接口控制器
 * 
 */
class attendanceAction extends apiAction {
	
	// ------------------------------------------------------------------------
	// 打卡签到相关
	// ------------------------------------------------------------------------

	/**
	 * 新增打卡记录
	 */
	public function check(){
		$type	= I('checkType');	//打卡类型
		if(! $type){
			$this->ajaxReturn(300, L('error'));
		}
		
		$data['userId']			= $this->user['id'];
		$data['storeId']		= $this->user['storeId'];
		$data['workDate']		= date('Y-m-d');
		$data[$type.'WorkTime']	= time();
		$data[$type.'WorkImage']= $this->uploadCheckImage(); //打卡图片
		$data[$type.'WorkGPS']	= I('gps');					//定位信息
		
		$module = D(MODULE_NAME);
		$where	= array('userId'=>$data['userId'], 'workDate'=>$data['workDate']);
		
		if($module->where($where)->find()){
			$res = $module->where($where)->save($data);
		}else{
			$res = $module->add($data);
		}
		
		if($res){
			$this->ajaxReturn(200, L('success'));
		}else{
			$this->ajaxReturn(300, L('error'));
		}
	}
	
	/**
	 * 我的签到历史记录
	 */
	public function checkHistory(){
		$month= I('month', date('Y-m'));
		$data = D(MODULE_NAME)->getCheckHistory($this->user['id'],$month);
		$this->ajaxReturn(200, L('success'), $data);
	}
			
	/**
	 * 检查用户今天打卡状态
	 */
	public function checkStatus(){
		$workDate = date('Y-m-d');
		$data = array('in'=>FALSE, 'inCheckImage'=>'', 'off'=>FALSE, 'offCheckImage'=>'');
		$res = D(MODULE_NAME)->where(array('userId'=>$this->user['id'], 'workDate'=>$workDate))->find();
		if($res){
			if(date('Y-m-d', $res['inWorkTime']) == $workDate){
				$data['in']				= TRUE;
				$data['inWorkImage']	= __ROOT__.'/'.C('pin_attach_path').$res['inWorkImage'];
			}
			if(date('Y-m-d', $res['offWorkTime']) == $workDate){
				$data['off']			= TRUE;
				$data['offWorkImage']	= __ROOT__.'/'.C('pin_attach_path').$res['offWorkImage'];
			}
		}
		$this->ajaxReturn(200, L('success'), $data);
	}
	
	/**
	 * 上传打卡图片
	 */
	public function uploadCheckImage(){
		 
		$dir = 'attendance/'.date('Ymd');
		if(! file_exists(C('pin_attach_path').$dir)){
			mkdir($dir, 0777);
		}
		$upload = $this->_upload($_FILES['checkImage'], $dir);
		
		if($upload['error']){
			$this->ajaxReturn(300, L('error'), $upload['info']);
		}else{
			$image = $dir.'/'.$upload['info'][0]['savename'];
		}
		return $image;
	}
	
	// ------------------------------------------------------------------------
	// 审核签到相关
	// ------------------------------------------------------------------------
	
	/**
	 * 获得审核签到主列表
	 */
	public function checkVerifyList(){
		$ids = D('admin')->get_subordinate_ids();
		$data= D('attendance')->getVerifyList($ids);
		$this->ajaxReturn(200, L('success'), $data);
	}
	
	/**
	 * 获得审核签到子列表
	 */
	public function checkVerifySubList(){
		$date	= I('date', date('Y-m-d'));
		$ids	= D('admin')->get_subordinate_ids();
		$data	= D('attendance')->getVerifySubList($ids,$date);
		$this->ajaxReturn(200, L('success'), $data);
	}
	
	/**
	 * 获得审核签记录详情
	 */
	public function checkVerifyDetail(){
		$checkId = I('checkId'); //打卡记录id
		if(! $checkId){
			$this->ajaxReturn(300, L('check_id_is_require'));
		}
		$data	= D('attendance')->getCheckDetail($checkId);
		//如果为未审核，则显示审核选项
		if($data['verifyType'] == 0){
			$data['verifyOptions'] =  D('attendance')->getVerifyOptions();
		}
		$this->ajaxReturn(200, L('success'), $data);
	}
	
	/**
	 * 打卡记录审核结果提交
	 */
	public function checkVerifySubmit(){
		$checkId = I('checkId'); //打卡记录id
		
		$data['verifyType']		= I('verifyTypeId'); //审核结果类型id
		$data['verifyRemark']	= I('verifyRemark'); //审核结果备注
		$data['verifyTime']		= time();	//审核时间
		
		if($checkId && $data['verifyType'] && D('attendance')->where(array('id'=>$checkId))->save($data)){
			$this->ajaxReturn(200, L('success'));
		}else{
			$this->ajaxReturn(300, L('error'));
		}
	}
}

/* End of file attendanceAction.class.php */